// dao/userDao.js
// ʵ����MySQL����
var mysql = require('mysql');
var $conf = require('../../conf/db');
var $util = require('../utils/util');
var $sql = require('../mapping/userInfoSqlMapping');

var pool  = mysql.createPool($util.extend({}, $conf.mysql));

module.exports = {
    queryAll: function (req,pageNo, beginIndex,pageSize, callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryAll, [parseInt(beginIndex),parseInt(pageSize)], function (err, result) {
                callback(result,pageNo);
                connection.release();
            });
        });
    },
    updatePersonInfo:function(req, res, callback) {
        pool.getConnection(function(err, connection) {
            var body=req.body;
            if(body.level==2){
                connection.query($sql.updatePersonInfo, [body.name,body.tel,body.email,body.birthday,body.address,body.sex,body.id], function(err, result) {
                    callback(result);
                    connection.release();
                });
            }else {
                connection.query($sql.updatePersonInfo, [body.name, body.tel, body.email, body.birthday, body.address, body.sex, req.session.currentUser.userInfoId], function (err, result) {
                    callback(result);
                    connection.release();
                });
            }
        });
    },

    queryInvite: function (req, res, callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryInvite, [], function (err, result) {
                if (result.length >= 1) {
                    result = {
                        code: 200,
                        content: result,
                        activeId:req.query.id,

                    };
                    callback(result);

                } else {
                    callback("no");
                }
                connection.release();
            });
        });
    },
    addUser:function (req, res, callback) {
        pool.getConnection(function(err, connection) {
           //管理员添加用户
            connection.query($sql.add, [ req.body.name, req.body.tel, req.body.email, req.body.birthday, req.body.address, req.body.sex], function(err, result) {
                if(result.affectedRows>=1) {
                    result = {
                        code: 200,
                        content:result
                    };
                    callback(result);

                }else{
                    callback("no");
                }
                // �ͷ�����
                connection.release();
            });
        });
    },
    add:function(req, res, callback) {
        pool.getConnection(function(err, connection) {
            connection.query($sql.add, ['新用户',req.param('username'),'','','',''], function(err, result) {
                if(result.affectedRows>=1) {

                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);
                } else {
                    callback("no");
                }
                connection.release();
            });
        });
    },

    queryById: function (req,res,result,  callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryById, result.userInfoId, function (err, result) {
                callback(result,req,res);
                connection.release();
            });
        });
    },
    //tool:
    queryByName: function (req,result,  callback) {   //管理员搜索（通过用户名）
        pool.getConnection(function (err, connection) {
        connection.query($sql.queryByName, ("%"+req.body.name+"%"), function (err, result) {
            callback(result);
            connection.release();
            });
        });
    },
    updateUser: function (req,result,  callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryPersonInfo, req.query.id, function (err, result) {
                callback(result);
                connection.release();
            });
        });
    },


    delUser:function (req, res, callback) {
        pool.getConnection(function(err, connection) {
            //管理员删除用户
            connection.query($sql.del, [ req.query.id], function(err, result) {
                if(result.affectedRows>=1) {
                    result = {

                        code: 200,
                        content:req.query.id
                    };
                    callback(result);

                }else{
                    callback("no");
                }
                // �ͷ�����
                connection.release();
            });
        });
    },

    queryTel: function (req, res, callback) {
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryTel, [req.body.param,req.body.id], function (err, result) {
                if (result.length >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);
                } else {
                    result = {
                        code: 0,
                        content: result
                    };
                    callback(result);
                }
                // �ͷ�����
                connection.release();
            });
        });
    },

    queryTelExist: function (req, res, callback) {   //判断电话号码是否使用
        pool.getConnection(function (err, connection) {
            connection.query($sql.queryTelExist, [req.body.param], function (err, result) {
                if (result.length >= 1) {
                    result = {
                        code: 200,
                        content: result
                    };
                    callback(result);
                } else {
                    result = {
                        code: 0,
                        content: result
                    };
                    callback(result);
                }
                // �ͷ�����
                connection.release();
            });
        });
    },


}
